﻿Imports MapWinGIS
Imports System.ComponentModel
<Serializable()> Public Class ESCriclePolyLayer
    Inherits ESPolylineLayer

    Public Sub New(ByVal ln As String, ByVal tl As BaseTool)
        MyBase.New(ln, tl)

       
    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub

 


    Public Overrides Sub AddEntityRange(ByVal lst As System.Collections.Generic.List(Of FEntity))

        For Each en As FEntity In lst

            dicKeyPts.Add(en.sn, en)
        Next


        Dim ptlist As MapWinGIS.Point()
        Dim sn As String
        Dim desc As String

        Dim sfPoly As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)



        sfPoly.StartEditingShapes(True)



        For Each en As FEntity In lst



            sn = en.sn
            ptlist = en.ptlist
            desc = en.desc


            If ptlist.Length < 1 Then

            Else


                Dim p1 As New Drawing.Point
                mapwin.ProjToPixel(ptlist(0).x, ptlist(0).y, p1.X, p1.Y)

                Dim p2 As New Drawing.Point
                mapwin.ProjToPixel(ptlist(1).x, ptlist(1).y, p2.X, p2.Y)

                Dim r As Double = Math.Sqrt((p1.X - p2.X) ^ 2 + (p1.Y - p2.Y) ^ 2)

                Dim mylist() As MapWinGIS.Point = Me.BuildCirclePoints(p1, r)





                '注意下面都是用mylist


                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYLINE
                Dim ptIndex As Integer = 0

                Dim n As Integer = mylist.Length

                '  Dim pTemp As MapWinGIS.Point

                For i As Integer = 1 To n

                    'pTemp = New MapWinGIS.Point

                    shp.InsertPoint(mylist(i - 1), ptIndex)


                Next




                Dim shpIndex As Integer = sfPoly.NumShapes

                sfPoly.EditInsertShape(shp, shpIndex)

                sfPoly.EditCellValue(0, shpIndex, sn)
                sfPoly.EditCellValue(1, shpIndex, desc)




            End If





        Next

        sfPoly.StopEditingShapes()


        '由于新增加了对象，所以需要根据现有状态，对对象操作

        Me.DrawText()

        mapwin.Redraw()

    End Sub

    Public Overrides Function AddEntity(ByVal sn As String, ByVal ptlist() As MapWinGIS.Point, ByVal desc As String) As Integer

        '关键节点，呵呵
        Dim en As New FEntity(sn, ptlist, desc)
        dicKeyPts.Add(sn, en)



        Dim p1 As New Drawing.Point
        mapwin.ProjToPixel(ptlist(0).x, ptlist(0).y, p1.X, p1.Y)

        Dim p2 As New Drawing.Point
        mapwin.ProjToPixel(ptlist(1).x, ptlist(1).y, p2.X, p2.Y)

        Dim r As Double = Math.Sqrt((p1.X - p2.X) ^ 2 + (p1.Y - p2.Y) ^ 2)

        Dim mylist() As MapWinGIS.Point = Me.BuildCirclePoints(p1, r)




        '前面只是为了画圆而作的预处理工作

        Dim sfPoly As Shapefile
        sfPoly = mapwin.get_GetObject(m_iLayer)


        sfPoly.StartEditingShapes(True)



        '注意下面都是用mylist


        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex As Integer = 0

        Dim n As Integer = mylist.Length

        '  Dim pTemp As MapWinGIS.Point

        For i As Integer = 1 To n

            'pTemp = New MapWinGIS.Point

            shp.InsertPoint(mylist(i - 1), ptIndex)


        Next




        Dim shpIndex As Integer = sfPoly.NumShapes

        sfPoly.EditInsertShape(shp, shpIndex)

        sfPoly.EditCellValue(0, shpIndex, sn)
        sfPoly.EditCellValue(1, shpIndex, desc)

        sfPoly.StopEditingShapes()

        '下面询问是否需要绘制标签

        Me.DrawText()



        mapwin.Redraw()

        Return shpIndex

    End Function


    Protected Function BuildCirclePoints(ByVal p1 As Drawing.Point, ByVal r As Double) As MapWinGIS.Point()
      



        '老板给出的另外的圆形测算方法
        Dim ptReturn As New List(Of Drawing.PointF)

        For i As Double = 0 To 2 * Math.PI Step Math.PI / 360
            Dim pTemp As New Drawing.PointF
            pTemp.X = r * Math.Cos(i) + p1.X
            pTemp.Y = r * Math.Sin(i) + p1.Y

            ptReturn.Add(pTemp)
        Next



        Dim plist(ptReturn.Count - 1) As MapWinGIS.Point


        Dim temp As MapWinGIS.Point

        Dim j As Integer = 0

        For Each p As Drawing.PointF In ptReturn

            temp = New MapWinGIS.Point
            mapwin.PixelToProj(p.X, p.Y, temp.x, temp.y)
            plist(j) = temp
            j = j + 1

        Next

        Return plist




       


    End Function


  
End Class
